﻿using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using WpfForPrism.Views;

namespace WpfForPrism.ViewModels
{
	internal class MainWindowViewModel : BindableBase
	{
		public DelegateCommand<string> ShowContentCmm { get; set; }

		public DelegateCommand<string> ShowDialogCmm { get; set; }

		/// <summary>
		/// 后退命令
		/// </summary>
		public DelegateCommand BackCmm { get; set; }

		/// <summary>
		/// 前进命令
		/// </summary>
		public DelegateCommand ForwardCmm { get; set; }

		/// <summary>
		/// 导航记录
		/// </summary>
		private IRegionNavigationJournal Journal;

		/// <summary>
		///区域管理
		/// </summary>
		private readonly IRegionManager RegionManager;

		/// <summary>
		/// 对话框服务
		/// </summary>
		public readonly IDialogService DialogService;

		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name="_RegionManager"></param>
		public MainWindowViewModel(IRegionManager _RegionManager, IDialogService _DialogService)
		{
			RegionManager = _RegionManager;
			DialogService = _DialogService;
			ShowContentCmm = new DelegateCommand<string>(ShowContentFunc);
			ShowDialogCmm = new DelegateCommand<string>(ShowDialogFunc);
			BackCmm = new DelegateCommand(Back);
			ForwardCmm = new DelegateCommand(Forward);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="view">用户空间名称</param>
		private void ShowDialogFunc(string view)
		{
			IDialogParameters paras = new DialogParameters();
			paras.Add("Title", "这是动态标题");
			paras.Add("para1", "业务参数1");
			paras.Add("para2", "业务参数2");
			DialogService.ShowDialog(view, paras, callback => 
			{ 
				if(callback.Result==ButtonResult.OK)
				{
					if (callback.Parameters.ContainsKey("result1"))
					{
						string r1 = callback.Parameters.GetValue<string>("result1");
					}
					if (callback.Parameters.ContainsKey("result2"))
					{
						string r2 = callback.Parameters.GetValue<string>("result2");
					}					
				}
			
			});
		}

		/// <summary>
		/// 前进命令
		/// </summary>
		/// <exception cref="NotImplementedException"></exception>
		private void Forward()
		{

			if (Journal != null && Journal.CanGoForward)
			{
				Journal.GoForward();
			}
		}

		/// <summary>
		/// 后退方法
		/// </summary>
		/// <exception cref="NotImplementedException"></exception>
		private void Back()
		{
			if (Journal != null && Journal.CanGoBack)
			{
				Journal.GoBack();
			}
		}

		/// <summary>
		/// 改变显示用户控件
		/// </summary>
		/// <param name="view"></param>
		private void ShowContentFunc(string view)
		{
			NavigationParameters paras = new NavigationParameters();
			paras.Add("MsgA", "大家好，我是A");
			RegionManager.Regions["ContentRegion"].RequestNavigate(view, callback =>
			{
				Journal = callback.Context.NavigationService.Journal;
			}, paras);
		}



		//private UserControl showContent;
		///// <summary>
		///// 显示的内容
		///// </summary>
		//public UserControl ShowContent
		//{
		//	get { return showContent; }
		//	set
		//	{
		//		showContent = value;
		//		RaisePropertyChanged(nameof(ShowContent));
		//	}
		//}
	}
}
